home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / bitsfunc / bits.c next >
C/C++ Source or Header  |  1990-10-27  |  3KB  |  99 lines

  1. /*--------------------------------------------------------------------------
  2. Module Name: BITS.C
  3.  
  4. Created By:  David Bernazzani
  5.  
  6. Create Date: 10/27/90
  7.  
  8. Purpose:     This module contains BIT manipulation routines.
  9.  
  10. ===========================================================================
  11.                           Revision History
  12. ===========================================================================
  13. Version    Date     Person         Description of Change
  14. -------  --------   ------   ----------------------------------------------
  15.  1.0     10/27/90    DSB     Initial Release.
  16. --------------------------------------------------------------------------*/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include "utils.h"
  20. #include "bits.h"
  21.  
  22. /*=========================================================================
  23. Function Name:  Bit_Test(...)
  24.  
  25. Purpose:  This routine will test to see if a particular bit is set
  26.           in a byte.
  27.  
  28. Inputs:   UBYTE val   --> Value to test bit in.
  29.           UBYTE bit   --> Bit to test (0-7).
  30.  
  31. Returns:  FLAG status --> TRUE if bit set, FALSE if not.
  32.  
  33. Version    Date     Person         Description of Change
  34. -------  --------   ------   ----------------------------------------------
  35.  1.0     10/27/90    DSB     Initial Release.
  36. --------------------------------------------------------------------------*/
  37. FLAG Bit_Test(UBYTE val, UBYTE bit)  /* bits range 0-7 */
  38. {
  39.    FLAG status = TRUE;
  40.    UBYTE test_val = 0x01;
  41.  
  42.    test_val = (test_val << bit);     /* Shift the bit into position */
  43.    if ((val & test_val) == 0)
  44.       status = FALSE;
  45.    return(status);
  46. }
  47.  
  48.  
  49. /*=========================================================================
  50. Function Name:  Bit_Set(...)
  51.  
  52. Purpose:  This routine set a particular bit in a byte.
  53.  
  54. Inputs:   UBYTE *val  --> Value to set bit in.   Updated for caller.
  55.           UBYTE bit   --> Bit to set (0-7).
  56.  
  57. Returns:  FLAG status --> TRUE.
  58.  
  59. Version    Date     Person         Description of Change
  60. -------  --------   ------   ----------------------------------------------
  61.  1.0     10/27/90    DSB     Initial Release.
  62. --------------------------------------------------------------------------*/
  63. FLAG Bit_Set(UBYTE *val, UBYTE bit)  /* bits range 0-7 */
  64. {
  65.    FLAG status = TRUE;
  66.    UBYTE test_val = 0x01;
  67.  
  68.    test_val = (test_val << bit);     /* Shift the bit into position */
  69.    *val = (*val | test_val);         /* Set that bit */
  70.    return(status);
  71. }
  72.  
  73.  
  74.  
  75. /*=========================================================================
  76. Function Name:  Bit_Clear(...)
  77.  
  78. Purpose:  This routine clear a particular bit in a byte.
  79.  
  80. Inputs:   UBYTE *val  --> Value to clear bit in.   Updated for caller.
  81.           UBYTE bit   --> Bit to clear (0-7).
  82.  
  83. Returns:  FLAG status --> TRUE.
  84.  
  85. Version    Date     Person         Description of Change
  86. -------  --------   ------   ----------------------------------------------
  87.  1.0     10/27/90    DSB     Initial Release.
  88. --------------------------------------------------------------------------*/
  89. FLAG Bit_Clear(UBYTE *val, UBYTE bit)  /* bits range 0-7 */
  90. {
  91.    FLAG status = TRUE;
  92.    UBYTE test_val = 0x01;
  93.  
  94.    test_val = (test_val << bit);     /* Shift the bit into position */
  95.    *val = (*val & (~test_val));      /* Clear that bit */
  96.    return(status);
  97. }
  98.  
  99.